package com.wangzh.nio.mytest;

import java.io.*;
import java.util.Properties;

public class Test1 {

    public static void main(String[] args) throws IOException {
//        拷贝一个文件夹
//        //1.创建
//        File src = new File("D:\\learn\\test");
//        //2.创建对象表示目的地
//        File dest = new File("D:\\learn\\test1");
//        copydir(src, dest);

        InputStream resourceAsStream = Test1.class.getClassLoader().getResourceAsStream("aa.txt");
        InputStreamReader  isr = new InputStreamReader(resourceAsStream);
        char[] buffer = new char[100];
        while (true){
            int read = isr.read(buffer);
            System.out.println(buffer);
        }
//        changeName(new File("D:\\typore\\img"));
    }
    /*
    获取当前目录下文件，按照指定规则修改文件名
     */
    private static void changeName(File src){
        File[] files = src.listFiles();
        for (File file : files) {
            String name = file.getName();
            if (name.startsWith("image-")) {

                System.out.println(file.getName());
                //去除中划线
                String replace = name.replace("-", "");
                File newName = new File(replace);
                System.out.println(newName);
                if (file.renameTo(newName)) {

                }else {
                    System.out.println("失败"+name);
                }
            }
        }
    }

    private static void copydir(File src, File dest) throws IOException {
        dest.mkdir();
        //递归
        //1.进入数据源
        File[] files = src.listFiles();
        //2.遍历数组
        for (File file : files) {
            if (file.isFile()) {
                //3.判断文件。拷贝
                FileInputStream fis = new FileInputStream(file);
                FileOutputStream fos = new FileOutputStream(new File(dest,file.getName()));

                byte[] bytes = new byte[1024];
                int len;
                while ((len = fis.read(bytes)) != -1) {
                    fos.write(bytes, 0, len);
                }
                fos.close();
                fis.close();
            } else {
                //4.判断文件夹。递归
                copydir(file,new File(dest,file.getName()));
            }
        }
    }
}
